﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using RentIt06WPFClient.UserSession;

namespace RentIt06WPFClient {
    /// <summary>
    /// Interaction logic for EditUser.xaml
    /// </summary>
    public partial class EditUser : Page {
        private Account acc;      
        
        public EditUser() {
            InitializeComponent();
        }
        private void Page_Loaded(object sender, RoutedEventArgs e) {
            updateFields();
        }
        private void updateFields() {
            acc = Session.User.ReadMyAccount();
            email.Text = acc.Email;
            firstName.Text = acc.FirstName;
            lastName.Text = acc.LastName;
            address.Text = acc.Street;
            zipCode.Text = acc.ZipCode;
            city.Text = acc.City;
            Country.Text = acc.Country;
            phoneNumber.Text = acc.PhoneNumber;
            balance.Content = "$" + acc.Credit;
        }

        private void updateButton_Click(object sender, RoutedEventArgs e) {
            if (password.Password == "") {
                msg.Content = "You must provide your current password";
                password.BorderBrush = Brushes.OrangeRed;
                return;
            }
            acc.Email = email.Text;
            acc.FirstName = firstName.Text;
            acc.LastName = lastName.Text;
            acc.Street = address.Text;
            acc.ZipCode = zipCode.Text;
            acc.City = city.Text;
            acc.Country = Country.Text;
            acc.PhoneNumber = phoneNumber.Text;
            try {
                Session.User.UpdateAccount(acc, password.Password);
                if(newPassword.Visibility == Visibility.Visible)
                    Session.User.ChangePassword(password.Password,newPassword.Password);
                Complete();
            } catch (FaultException ex) {
                msg.Content = ex.Message;
            }
        }
        private void Complete() {
            updateButton.Content = "Updated!";
            updateButton.IsEnabled = false;
            msg.Foreground = Brushes.Green;
            msg.Content = "Changes succesfully updated!";
        }
        
        private void backButton_Click(object sender, RoutedEventArgs e) {
            NavigationService.GoBack();
        }

        private void changePasswordButton_Click(object sender, RoutedEventArgs e) {
            changePasswordButton.Visibility = Visibility.Hidden;
            newPassword.Visibility = Visibility.Visible;
            newPassword.Focus();
        }

        private void password_PasswordChanged(object sender, RoutedEventArgs e) {
            password.BorderBrush = Brushes.Black;
        }

        private void CancelChangePWButton_Click(object sender, RoutedEventArgs e) {
            changePasswordButton.Visibility = Visibility.Visible;
            newPassword.Visibility = Visibility.Hidden;
        }

        private void addCredits_Click(object sender, RoutedEventArgs e) {
            var addCreditWindow = new AddCredit(0);
            addCreditWindow.Closed += ((sender1, e1) => updateFields());
            addCreditWindow.Show();
        }
    }
}
